package com.cysyz.sql.column.html.mapper.impl;

import cn.org.rapid_framework.generator.provider.db.table.model.Column;
import com.cysyz.mylib.freemarker.FreeMarkerSupportTool;
import com.cysyz.sql.column.html.mapper.ColumnHtmlInputMapper;
import com.cysyz.sql.column.html.mapper.validate.ColumnHtmlValidateInfoMapper;
import com.cysyz.sql.column.html.mapper.validate.impl.RequireColumnHtmlValidateInfoMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2015-4-23.
 */
public abstract class BaseColumnHtmlInputMapper implements ColumnHtmlInputMapper<Column> {
    private List<String> supportList = new ArrayList<String>();

    private List<ColumnHtmlValidateInfoMapper> columnHtmlValidateInfoMappers = new ArrayList<ColumnHtmlValidateInfoMapper>();

    public BaseColumnHtmlInputMapper() {
        setSupportList(this.supportList);
        columnHtmlValidateInfoMappers.add(new RequireColumnHtmlValidateInfoMapper());
    }

    @Override
    public boolean support(Column obj) {
        if (supportList.contains(obj.getJavaType())) {
            return true;
        }
        return false;
    }

    @Override
    public void execute(List args) {
        this.setHtmlInput((Column) args.get(0));
    }

    public void setHtmlInput(Column column) {

        for (ColumnHtmlValidateInfoMapper columnHtmlValidateInfoMapper : columnHtmlValidateInfoMappers) {
            if (columnHtmlValidateInfoMapper.support(column)) {
                columnHtmlValidateInfoMapper.setValidateInfos(column);
                break;
            }
        }

        FreeMarkerSupportTool fs = new FreeMarkerSupportTool("/ftl/htmlinput");

        Map<String, Object> dataMap = setDataMap(column);
        String htmlInput = fs.generate(setTemplateName(), dataMap);

        column.setHtmlInput(htmlInput);
    }

    private Map<String, Object> setDataMap(Column column) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("column", column);
        return dataMap;
    }

    protected abstract String setTemplateName();

    protected abstract void setSupportList(List<String> supportList);

}
